package com.GUIWork;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPasswordField;
import javax.swing.JTextPane;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import com.dbConnection.*;

public class LoginFrame extends JFrame {
	private JTextField txtUsername;
	private JPasswordField passwordField;
	LoginFrame lframe = this;
	theConnection conn;
	
	public LoginFrame(final theConnection conn) {
		this.conn = conn;
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.controlHighlight);
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblGtbay = new JLabel("GTBay");
		lblGtbay.setForeground(new Color(255, 215, 0));
		lblGtbay.setHorizontalAlignment(SwingConstants.CENTER);
		lblGtbay.setFont(new Font("Tahoma", Font.PLAIN, 24));
		lblGtbay.setBounds(138, 41, 127, 53);
		panel.add(lblGtbay);
		
		txtUsername = new JTextField();
		txtUsername.setText("Username");
		txtUsername.setBounds(165, 105, 86, 20);
		panel.add(txtUsername);
		txtUsername.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(165, 136, 86, 20);
		panel.add(passwordField);
		passwordField.setText("Password");
		passwordField.setColumns(10);
		
		JTextPane txtpnItsNotTrash = new JTextPane();
		txtpnItsNotTrash.setBackground(SystemColor.controlHighlight);
		txtpnItsNotTrash.setEditable(false);
		txtpnItsNotTrash.setText("It's not trash if someone will buy it!");
		txtpnItsNotTrash.setBounds(165, 167, 104, 51);
		panel.add(txtpnItsNotTrash);
		
		final JLabel error = new JLabel("");
		error.setBounds(284, 124, 98, 14);
		panel.add(error);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JSplitPane splitPane_1 = new JSplitPane();
		panel_1.add(splitPane_1);
		
		JSplitPane splitPane_2 = new JSplitPane();
		splitPane_1.setRightComponent(splitPane_2);
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				conn.close();
				lframe.dispose();
				System.exit(0);
			}
		});
		splitPane_2.setLeftComponent(btnClose);
		
		JButton btnNewUser = new JButton("New User?");
		btnNewUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				newUserFrame uframe = new newUserFrame(conn);
				lframe.dispose();
				uframe.setVisible(true);
				uframe.setBounds(100, 100, 450, 300);
				uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
		splitPane_2.setRightComponent(btnNewUser);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				short login = doLogin();
				if(login == 1){
					RegUserFrame uframe = new RegUserFrame(conn);
					uframe.setVisible(true);
					uframe.setBounds(100, 100, 450, 300);
					uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					lframe.dispose();
				}
				else if(login ==2){
					conn.askDB("SELECT Title FROM AdminUser WHERE Username = "+txtUsername.getText());
					String title = "Not an Admin";
					try {
						title = conn.getLastQuery(1).get(0);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					AdminFrame aframe = new AdminFrame(conn, title);
					aframe.setVisible(true);
					aframe.setBounds(100, 100, 450, 300);
					aframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					lframe.dispose();
				}
				else
					error.setText("Username/password Incorrect");
			}
		});
		splitPane_1.setLeftComponent(btnSubmit);
	}
	
	public short doLogin(){
		String username = txtUsername.getText();
		String password = new String(passwordField.getPassword());
		conn.askDB("SELECT Username FROM User WHERE Username = " + username + " AND Password = " + password);
		
		return 2;
	}
}
